package visualization.graphkeymodel.piechart;

import java.awt.BasicStroke;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import visualization.PairEntry;
import visualization.Visualization;
import visualization.graphkeymodel.GraphKeyModel;


@SuppressWarnings("serial")
/**
 * This class creates a pie chart that pairs a string value
 *  to a double representation of its value in the pie.
 * @author Ben Schwab
 *
 */
public class PieChart extends GraphKeyModel {
    private static final int MAX_DEGREES = 360;
    private static final int STROKE_WEIGHT = 10;
    private ArrayList<PieSlice> myPieSlices = new ArrayList<PieSlice>();
    private double myTotalValue;
    private int myRadius;
    private List<PairEntry<String, Double>> myEntries;

    /**
     * 
     * @param entries paired string and data entries for pie chart
     * @param preferredSize the size of this graph
     */
    public PieChart (List<PairEntry<String, Double>> entries, Dimension preferredSize) {
        super(preferredSize);
        myEntries = entries;
        createNewColorGenerator(entries.size());
        generateRandomColors();
        calculateAreas();
        int pieWidth = (int) getGraphArea().getWidth();
        int pieHeight = (int) getGraphArea().getWidth();
        myRadius = Math.min(pieWidth, pieHeight);
        generatePieSlices(entries);
        generateColorKey(entries);
        generateTitle();
        repaint();
    }

    private void generateColorKey (List<PairEntry<String, Double>> entries) {
        ArrayList<String> titles = new ArrayList<String>();
        for (PairEntry<String, Double> entry : entries) {
            titles.add(entry.getMyTitle());
        }
        super.generateColorKey(titles);

    }

    private void generatePieSlices (List<PairEntry<String, Double>> entries) {
        int counter = 0;
        myTotalValue = 0;
        Color[] colors = getColors();
        for (PairEntry<String, Double> entry : entries) {
            entry.getMyTitle();
            myPieSlices.add(new PieSlice(entry.getMyTitle(), entry.getMyInfo(), colors[counter],
                                         getGraphArea()));
            counter++;
            myTotalValue += entry.getMyInfo();
        }
    }

    @Override
    public Visualization randomizeColors () {
        return new PieChart(myEntries, getDimension());
    }

    @Override
    public void paintComponent (Graphics g) {
        super.paintComponent(g);
        Graphics2D pen = (Graphics2D) g;
        getTitle().paint(pen);
        paintPieChart(pen);
        getColorKey().paint(pen);
    }

    private void paintPieChart (Graphics2D pen) {
        int startAngle = 0;
        int counter = 0;
        paintPieOutline(pen);
        for (PieSlice slice : myPieSlices) {
            int arcAngle;
            if (counter == myPieSlices.size() - 1) {
                arcAngle = MAX_DEGREES - startAngle;
            }
            else {
                arcAngle = (int) (slice.getValue() / myTotalValue * MAX_DEGREES);
            }
            slice.paint(pen, startAngle, arcAngle);
            startAngle += arcAngle;
            counter++;
        }

    }

    private void paintPieOutline (Graphics2D pen) {
        BasicStroke s = new BasicStroke(STROKE_WEIGHT);
        pen.setStroke(s);
        pen.drawOval((int) getGraphArea().getMinX(), (int) getGraphArea().getMinY(), myRadius,
                     myRadius);
    }

}
